const log = require('../util/log'),
  config = require('../util/config'),
  chromeMng = require('../mng/chromeMng')

module.exports = (req, res) => {
  require('../util/service').handlePost(req, res, async (reqObj, resolve) => {
    if (!reqObj.q) {
      resolve({
        result: -1,
        msg: '缺少q'
      })
      return
    }
    if (config.domain == '') {
      chromeMng.getTaobaoItems(reqObj).then(list => {
        resolve({
          list: list
        })
      }, result => {
        log(result)
        resolve({
          result: -1,
          msg: result
        })
      })
    } else if (config.domain == 'zoro.site') {
      require('../util/getHtml')({
        url: require('../mng/chromeServerMng').get() + 'getTaobaoItem.do',
        data: JSON.stringify(reqObj)
      }).then(result => {
        var obj = JSON.parse(result)
        if (obj.list) {
          obj.list.forEach(item => {
            item.rate = Math.round(item.rate * config.returnRate * 100) / 100
          })
        }
        resolve(obj)
      }, result => {
        logger.error('[' + __filename + ':' + __line + ']', result)
        resolve({
          result: -1,
          msg: '系统错误'
        })
      })
    }
  })
}
